import torch
import numpy as np
import re

class Net(torch.nn.Module):
    def __init__(self, n_feature, n_output):
        super(Net, self).__init__()
        self.predict = torch.nn.Linear(n_feature, n_output)

    def forward(self, x):
        out = self.predict(x)
        return out

ff = open("./boston.data").readlines()
data = []
for item in ff:
    out = re.sub(r"\s+", " ", item.strip())
    data.append(out.split(" "))
data = np.array(data, dtype=np.float32)

Y = data[:, -1]
X = data[:, 0:-1]

X_train = X[0:496]
Y_train = Y[0:496]
X_test = X[496:, ...]
Y_test = Y[496:, ...]

net = torch.load("model/boston_model.pkl", weights_only=False)
loss_func = torch.nn.MSELoss()

x_data = torch.tensor(X_test, dtype=torch.float32)
y_data = torch.tensor(Y_test, dtype=torch.float32)
pred = net.forward(x_data)
pred = torch.squeeze(pred)
loss = loss_func(pred, y_data) * 0.001

print("Test loss:{}".format(loss))  # ==> Test loss:0.028156790882349014
